#!/usr/bin/env python3 
# -*- coding: utf-8 -*-
# @Author  : Grit<dooms21day@163.com>
# @Company : 成都慧创智通信息技术有限公司
# @Brand   : 企慧蚁
# @Copyright: 2021-
import json

from odoo import http
import logging

_logger = logging.getLogger(__name__)
from odoo.http import request
from odoo.addons.web.controllers.main import DataSet
from lxml import etree as etree


class Controller(DataSet, http.Controller):

    @http.route(['/web/dataset/call_kw', '/web/dataset/call_kw/<path:path>'], type='json', auth="user")
    def call_kw(self, model, method, args, kwargs, path=None):
        call_kw_result = super(Controller, self).call_kw(model, method, args, kwargs, path)
        if method == "load_views" and call_kw_result.get('fields_views').get('list'):
            view_id = call_kw_result["fields_views"]["list"].get("view_id")
            action_id = kwargs['options'].get("action_id")
            if view_id and action_id:
                fields_info = call_kw_result['fields_views']['list']['fields'].keys()
                field_configs = request.env['field.config'].search([
                    ('view_id', '=', view_id),
                    ('act_id', '=', action_id),
                    ('user_id', '=', request.env.uid),
                    ('invisible', '=', False),
                ])
                for field_config in field_configs:
                    if field_config not in fields_info:
                        fieldinfo = call_kw_result['fields'].get(field_config.name, False)
                        if fieldinfo:
                            call_kw_result['fields_views']['list']['fields'][field_config.name] = fieldinfo
        return call_kw_result